﻿/// <summary>
/// <Filename>: Motion_AutoAssemblyAndTime.cs
/// Author: Jiang Xiaolong
/// Created: 2015.02.02
/// Version: 1.0
/// Company: Sunnytech
/// Function: 自动装配表格生成和时间信息处理类
///
/// Changed By:
/// Modification Time:
/// Discription:
/// <summary>
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Data;
using Motion.MotionPlatform.Tools;

namespace Motion.MotionPlatform
{
	public class Motion_AutoAssemblyAndTime
	{

		public static float tableTotalTime = 0f;


		public Motion_AutoAssemblyAndTime()
		{
			
		}
		
		/// <summary>
		/// 初始生成Z文件夹和ZID表格；
		/// 先删除ZID，再复制CID；
		/// 然后将新的ZID表格中的内容倒排，将第一列改成ZN系列（此处可能要遍历修改两次）；
		/// </summary>
		/// <param name="target_path">当前案例根目录；</param>
		public static void ChartZIDInit()
		{
			string target_path = Motion_Data.taskRootPath;
			//检测是否存在Z文件夹
			if (!Directory.Exists(target_path + "/Z"))
			{
				Directory.CreateDirectory(target_path + "/Z");
			}
			//检测ZID是否存在，存在删除
			if (File.Exists(target_path + "/Z/ZID.xls"))
			{
				File.Delete(target_path + "/Z/ZID.xls");
			}
			//检测CID表格是否存在
			if (File.Exists(target_path + "/C/CID.xls"))
			{
				File.Copy(target_path + "/C/CID.xls", target_path + "/Z/ZID.xls", true);
				
				ExcelOperator excelWriter = new ExcelOperator();
				DataTable origin_file = excelWriter.ExcelReader(target_path + "/Z/ZID.xls", "ID");
				if (origin_file == null || origin_file.Rows.Count == 0)
				{
					Debug.LogError(target_path + "/Z/ZID.xls" + "，该表格为空，无法进行自动装配初始化，请停止运行并进行检查，然后继续！");
					return;
				}
				//将CN列改成ZN列
				string[] contents = new string[origin_file.Columns.Count];
				for (int i = 0; i < origin_file.Rows.Count; i++)
				{
					for (int j = 0; j < origin_file.Columns.Count; j++)
					{
						if (j != 0)
						{
							contents[j] = origin_file.Rows[i][j].ToString();
						}
						else
						{
							contents[j] = "Z" + (i + 1).ToString();
						}
					}
					excelWriter.UpdateData(target_path + "/Z/ZID.xls", "ID", contents, 1);
				}
				//倒排
				InvertChart(target_path + "/Z/ZID.xls", "ID", 0);
			}
			else
			{
				Debug.LogError(target_path + "/C/CID.xls，该原始表格不存在，无法继续进行自动装配生成，请停止运行并进行检查，然后继续！");
				return;
			}
		}
		
		/// <summary>
		/// 初始生成ZN表格
		/// 先删除ZN，再复制CN，生成Z（COUNT - N）；
		/// 然后将新的ZN表格中MAIN的内容倒排；
		/// </summary>
		/// <param name="target_path">当前案例根目录；</param>
		/// <param name="id_list">所有C表列表；</param>
		public static void ChartZNInit()
		{
			string target_path = Motion_Data.taskRootPath;
			for (int i = 0; i < Motion_Data.IDChartCount; i++)
			{
				//查看原始表格是否存在
				if (File.Exists(target_path + "/C/C" + (i + 1).ToString() + ".xls"))
				{
					//如果那个ZN表格在的话删除它
					if (File.Exists(target_path + "/Z/Z" + (Motion_Data.IDChartCount - i).ToString() + ".xls"))
					{
						File.Delete(target_path + "/Z/Z" + (Motion_Data.IDChartCount - i).ToString() + ".xls");
					}
					File.Copy(target_path + "/C/C" + (i + 1).ToString() + ".xls", target_path + "/Z/Z" + (Motion_Data.IDChartCount - i).ToString() + ".xls", true);
					//倒排MAIN表内容
					InvertChart(target_path + "/Z/Z" + (Motion_Data.IDChartCount - i).ToString() + ".xls", "MAIN", Motion_Data.IDColIndex);
				}
				else
				{
					Debug.LogError(target_path + "/C/C" + (i + 1).ToString() + ".xls，该原始表格不存在，无法继续进行自动装配生成，请停止运行并进行检查，然后继续！");
					return;
				}
			}
		}

		//将表格内容倒排，因为需要一列主键，那一列将不会发生变化；
		private static void InvertChart(string file_path, string sheet_name, int main_col)
		{
			if (File.Exists(file_path))
			{
				ExcelOperator excelOp = new ExcelOperator();
				DataTable origin_file = excelOp.ExcelReader(file_path, sheet_name);
				if (origin_file == null || origin_file.Rows.Count == 0)
				{
					Debug.LogError(file_path + "，该表格为空，无法进行内容倒排，请停止运行并进行检查，然后继续！");
					return;				
				}
				if (origin_file.Columns.Count - 1 < main_col)
				{
					Debug.LogError(file_path + "，该表格总列数为： " + origin_file.Columns.Count + "，主键列索引为："+ main_col + "，该索引值超出列数范围，请停止运行并进行检查，然后继续！");
					return;
				}
				string[,] contents = new string[origin_file.Rows.Count, origin_file.Columns.Count];
				//倒排
				for (int i = origin_file.Rows.Count - 1; i >= 0; i--)
				{
					for (int j = 0; j < origin_file.Columns.Count; j++)
					{
						if (j == main_col)
						{
							contents[i, j] = origin_file.Rows[origin_file.Rows.Count - 1 - i][j].ToString();
						}
						else
						{
							contents[i, j] = origin_file.Rows[i][j].ToString();
						}
					}
				}
				excelOp.UpdateData(file_path, sheet_name, contents, main_col);
			}
			else
			{
				Debug.LogError(file_path + "，该原始表格不存在，无法进行表格内容倒排，请停止运行并进行检查，然后继续！");
			}
		}

		/// <summary>
		/// 自动更新装配表
		/// </summary>
		/// <param name="contents"></param>
		/// <param name="sheet_name"></param>
		/// <param name="main_col"></param>
		public static void AutoUpdate(string[] contents, string sheet_name, int main_col)
		{
			string filePath = Motion_Data.taskRootPath + "/Z/Z" + (Motion_Data.IDChartCount - Motion_Data.CurrentExcelIndex).ToString() + ".xls";
			ExcelOperator excelOp = new ExcelOperator();
			excelOp.UpdateData(filePath, sheet_name, contents, main_col);
		}

		/// <summary>
		/// 时间更新；
		/// </summary>
		/// <param name="contents"></param>
		/// <param name="sheet_name"></param>
		/// <param name="main_col"></param>
		public static void AutoTimeUpdate(string[] contents, string sheet_name, int main_col, bool is_id)
		{
			string filePath = Motion_Data.taskRootPath + "/C/C" + (Motion_Data.CurrentExcelIndex + 1).ToString() + ".xls";

			if(Motion_Data.CurrentBranch != ""){
				filePath = Motion_Data.taskRootPath + "/C/C" + (Motion_Data.CurrentExcelIndex + 1).ToString() + "_" + Motion_Data.CurrentBranch + ".xls";
			}

			if (is_id)
			{
				filePath = Motion_Data.taskRootPath + "/C/CID.xls";
			}
			ExcelOperator excelOp = new ExcelOperator();
			excelOp.UpdateData(filePath, sheet_name, contents, main_col);
		}
	}


}

